//
//  ForgetPasswordViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/14.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "ForgetPasswordNextViewController.h"
@interface ForgetPasswordViewController ()<UITextFieldDelegate>
{
    NSTimer * timer;
    
    NSMutableArray * nArr;
    
    NSMutableString * phoneNumStr;
    NSMutableString * passWordStr;
}
@end

@implementation ForgetPasswordViewController
static int  timeri = 60;
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    timeri = 60;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    // Do any additional setup after loading the view.
}
-(void)main
{
    phoneNumStr = [[NSMutableString alloc]init];
    passWordStr = [[NSMutableString alloc]init];
    
    nArr = [[NSMutableArray alloc]init];
    for (int i = 0; i<10; i++) {
        [nArr addObject:[NSString stringWithFormat:@"%i",i]];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    //背景图
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * bgImgV = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgImgV.image = [UIImage imageNamed:@"登录"];
    [self.view addSubview:bgImgV];
    //上面3
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 20)];
    titleLab.text = @"忘记密码";
    titleLab.textColor = UIColorFromRGB(0xFFFFFF);
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont systemFontOfSize:23];
    [self.view addSubview:titleLab];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 30, 30, 20);
    [leftBtn setImage:[UIImage imageNamed:@"上月"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnActoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    
    //textField
    NSArray * imgArr = @[@"手机icon",@"验证码icon"];
    NSArray * titleArr = @[@"手机号",@"验证码"];
    
    for (int i = 0; i<2; i++) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(29), titleLab.bottom+SIZETO6H(53)+i*(47+SIZETO6H(15)), SCREEN_WIDTH-SIZETO6W(29)*2, 47)];
        view.backgroundColor = [UIColor clearColor];
        view.layer.masksToBounds = YES;
        view.layer.borderColor = [UIColorFromRGB(0xFFFFFF) CGColor];
        view.layer.borderWidth = 1.0f;
        view.layer.cornerRadius = 20;
        [self.view addSubview:view];
        
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(20), view.height/2-18/2, 15, 20)];
        imgV.image = [UIImage imageNamed:imgArr[i]];
        [view addSubview:imgV];
        
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(imgV.right+SIZETO6W(15), 0, view.width-SIZETO6W(20)-SIZETO6W(15)-15, 49)];
        UIColor * color = UIColorFromRGB(0xffffff);
        textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:titleArr[i] attributes:@{NSForegroundColorAttributeName:color}];
        textField.tag = i+100;
        [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        textField.textColor = color;
        [view addSubview:textField];
        if (i == 0) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 0, 30, 30);
            [btn setImage:[UIImage imageNamed:@"关闭按钮-"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            textField.rightView = btn;
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.rightViewMode = UITextFieldViewModeWhileEditing;
        }
        else
        {
            textField.delegate = self;
            textField.keyboardType = UIKeyboardTypeASCIICapable;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 3, 100, 41);
            [btn setTitle:@"发送验证码" forState:UIControlStateNormal];
            btn.tag = 712;
            [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
            [btn setBackgroundColor:UIColorFromRGB(0x4A6649)];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 20;
            [btn addTarget:self action:@selector(sendMSGActino:) forControlEvents:UIControlEventTouchUpInside];
            textField.rightView = btn;
            textField.rightViewMode = UITextFieldViewModeAlways;
            
            UIButton * lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            lastBtn.frame = CGRectMake(SIZETO6W(29), view.bottom+SIZETO6H(51), SCREEN_WIDTH-SIZETO6W(29)*2, 47);
            [lastBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
            [lastBtn setTitle:@"下一步" forState:UIControlStateNormal];
            lastBtn.backgroundColor = UIColorFromRGB(0x6ec844);
            lastBtn.layer.masksToBounds = YES;
            lastBtn.layer.cornerRadius = 20;
            [lastBtn addTarget:self action:@selector(lastBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:lastBtn];
        }
        
    }
    
}
-(void)textChange:(UITextField *)textField
{
    UITextField * passWordTF = (UITextField *)[self.view viewWithTag:101];
    if (textField.tag == 100) {
        if (textField.text.length > phoneNumStr.length) {
            if (textField.text.length<=11) {
                NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
                if ([nArr containsObject:s] ) {
                    [phoneNumStr appendString:s];
                }
            }
        }
        else if([textField.text isEqualToString:@""])
        {
            phoneNumStr = [[NSMutableString alloc]init];
            passWordStr = [[NSMutableString alloc]init];
            passWordTF.text = @"";
        }
        else
        {
            NSString * str = [phoneNumStr substringToIndex:(phoneNumStr.length-1)];
            phoneNumStr = [[NSMutableString alloc]initWithString:str];
        }
        
        textField.text = phoneNumStr;
    }
    else
    {
        if (textField.text.length > passWordStr.length) {
            if (textField.text.length <= 6) {
                
                NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
                
                [passWordStr appendString:s];
                
            }
        }
        else if([textField.text isEqualToString:@""])
        {
            passWordStr = [[NSMutableString alloc]init];
        }
        else
        {
            NSString * str = [passWordStr substringToIndex:passWordStr.length-1];
            passWordStr = [[NSMutableString alloc]initWithString:str];
        }
        textField.text = passWordStr;
    }
}
#pragma mark - btn的点击事件们
-(void)leftBtnActoin:(id)sender
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)clearBtnClick:(id)sender
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    textField.text = @"";
    UITextField * passWordTF = (UITextField *)[self.view viewWithTag:101];
    passWordTF.text = @"";
    
    phoneNumStr = [[NSMutableString alloc]init];
    passWordStr = [[NSMutableString alloc]init];
    
}
-(void)sendMSGActino:(UIButton *)btn
{
    UITextField * phoneNumTextfield = (UITextField *)[self.view viewWithTag:100];
    __weak typeof(self)weakSelf = self;
    if (![phoneNumTextfield.text isEqualToString:@""]) {
        [U76HONet sendMessageWithAction:@"send2" mobile:phoneNumTextfield.text andDataBlock:^(NSDictionary *dic) {
            [Singleton alerSendMessage:@"发送成功"];
            btn.enabled = NO;
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(timerAction) userInfo:nil repeats:YES];

        }];
    }
    else
    {
        [Singleton alerSendMessage:@"手机号不能为空"];
        
    }

    
}
-(void)timerAction
{
    UIButton * btn = (UIButton *)[self.view viewWithTag:712];

    if (timeri>=0) {
        [btn setTitle:[NSString stringWithFormat:@"%is",timeri --] forState:UIControlStateNormal];
    }
    else
    {
        [timer invalidate];
        timeri = 60;
        [btn setTitle:@"发送验证码" forState:UIControlStateNormal];
        btn.enabled = YES;
    }
}
#pragma  mark -- 下一步
-(void)lastBtnAction:(UIButton *)btn
{
    ForgetPasswordNextViewController * forgetNextPasswordVC = [[ForgetPasswordNextViewController alloc]init];
    
   // [self.navigationController pushViewController:forgetNextPasswordVC animated:YES];

    //电话号码
    UITextField * phoneNumTextfield = (UITextField *)[self.view viewWithTag:100];
    forgetNextPasswordVC.mobile = phoneNumTextfield.text;
    //验证码待定
    UITextField * msgCodeTextField = (UITextField *)[self.view viewWithTag:101];
    
    if ([phoneNumTextfield.text isEqualToString:@""] || [msgCodeTextField.text isEqualToString:@""]) {
        [Singleton alerSendMessage:@"不能为空"];
    }
    else
    {
        [U76HONet messageCorrectWithaction:@"" mobile:phoneNumTextfield.text code:msgCodeTextField.text andDataBlock:^(NSDictionary *dic) {
            
           // [self presentViewController:forgetNextPasswordVC animated:YES completion:nil];
            [self.navigationController pushViewController:forgetNextPasswordVC animated:YES];
        }];
    }

    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        //        NSLog(@"开始登录了");
        
        ForgetPasswordNextViewController * forgetNextPasswordVC = [[ForgetPasswordNextViewController alloc]init];
        
        //电话号码
        UITextField * phoneNumTextfield = (UITextField *)[self.view viewWithTag:100];
        forgetNextPasswordVC.mobile = phoneNumTextfield.text;
        //验证码待定
        UITextField * msgCodeTextField = (UITextField *)[self.view viewWithTag:101];
        
        if ([phoneNumTextfield.text isEqualToString:@""] || [msgCodeTextField.text isEqualToString:@""]) {
            [Singleton alerSendMessage:@"不能为空"];
        }
        else
        {
            [U76HONet messageCorrectWithaction:@"" mobile:phoneNumTextfield.text code:msgCodeTextField.text andDataBlock:^(NSDictionary *dic) {
                [self.navigationController pushViewController:forgetNextPasswordVC animated:YES];
            }];
        }
        return NO;
    }
    return YES;
    
    
    
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)dealloc
{
    [timer invalidate];
    NSLog(@"deale0000000");
    NSLog(@"销毁了");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
